import BaseItemView from "../../../cfw/mvc/BaseItemView";
import SDKManager from "../../SDKManager";
import RandomHelper from "../../../cfw/tools/RandomHelper";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class MatrixSingleItemView extends BaseItemView {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property
    time: number = 0.2;

    @property
    angle: number = 10;

    start() {
        this.refresh()
    }

    refresh() {
        let list = SDKManager.getChannel().getAdData()
        if (list && list.length > 0) {
            this.setModel(list[RandomHelper.random(0, list.length)])
            if(this.model){
                ModuleManager.publicLoader().loadRes(this.model.getGameIcon(), ResType.Remote, (err, tex: ResItem) => {
                    if (err || !cc.isValid(this.node)) {
                        return;
                    }
                    this.icon.spriteFrame = new cc.SpriteFrame(tex.getRes())
                    this.setTimeout(() => {
    
                        this.refresh()
                        cc.tween(this.icon.node).
                            by(this.time, { angle: this.angle })
                            .by(this.time, { angle: -this.angle})
                            .start();
                    }, 3000)
                })
            }

        }

    }

    onButtonClick() {
        SDKManager.getChannel().navigate2Mini(this.model)
    }
    // update (dt) {}
}
